package com.czl.category.test.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.czl.category.test.entity.UUser;
import com.czl.category.test.service.IUUserService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Auto-creativity
 * @since 2021-01-18
 */
@RestController
@RequestMapping("/u-user")
public class UUserController {

    
    @Autowired
    private IUUserService iUUserService;

    
    /**
    * 通过id查询
    */
    @GetMapping("/get-by-id/{id}")
    public ResponseEntity<UUser> getById(@PathVariable(value = "id") Integer id){
        return ResponseEntity.ok(iUUserService.getById(id));
    }

    /**
    * 新增
    */
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody UUser uUser){
        System.out.println("=========");
        iUUserService.save(uUser);
        return ResponseEntity.ok("ok");
    }

    /**
    * 通过id删除
    */
    @DeleteMapping("/delete-by-id/{id}")
    public ResponseEntity<String> delete(@PathVariable(value = "id") String ids){
        String[] idsStrs = ids.split(",");
        for (String id:idsStrs){
            iUUserService.removeById(Integer.parseInt(id));
        }
        return ResponseEntity.ok("ok");
    }

    /**
    * 修改
    */
    @PutMapping("/update")
    public ResponseEntity<String> updateById(@RequestBody UUser uUser){
        iUUserService.updateById(uUser);
        return ResponseEntity.ok("ok");
    }


    /**
    * 查询列表
    */
    @PostMapping("/list")
    public ResponseEntity<String> list(){
//    final LambdaQueryWrapper<UUser> lambda = new QueryWrapper<UUser>().lambda();
//        this.buildCondition(lambda,uUser);
        return ResponseEntity.ok("");
    }

//    /**
//    * 分页查询
//    */
//    @PostMapping("/page")
//    public Page<UUserReqVo> page(@RequestBody Page<UUserReqVo> pageParam){
//        final UUserReqVo param = pageParam.get();
//        final LambdaQueryWrapper<UUser> lambda = new QueryWrapper<UUser>().lambda();
//        this.buildCondition(lambda,param);
//        final IPage<UUser> page = iUUserService.page(new Page<UUserReqVo>(pageParam.getPageNo(), pageParam.getPageSize()), lambda);
//
//
//        // 封装Page对象
//        Page<UUserReqVo> pages = Page.wrap(new ArrayList<>());
//        PageUtil.setPagesByHands(pageParam, UUserReqVo, pages);
//
//
//        //pageNo当前页，pageSize每页显示记录数
//        /*com.github.pagehelper.Page<T> page = null;
//        if(pageNo!=-1 && pageSize!=-1){
//            page = PageHelper.startPage(pageNo, pageSize);
//        }*/
//
//       /* PageResult<UUser> pr = new PageResult();
//        pr.setPageCount(page.getPages());
//        pr.setTotalCount(page.getTotal());
//        pr.setPageNo(new Long(page.getCurrent()).intValue());
//        pr.setPageSize((int) page.getSize());
//        pr.setResults(page.getRecords());*/
//       // return Response.success(pr);
//    }


        /**
        * 构造查询条件
        * @param lambda
        * @param param
        */
        private void buildCondition(LambdaQueryWrapper<UUser> lambda, UUserReqVo param){
            if(!StringUtils.isEmpty(param.getId())){
                lambda.eq(UUser::getId, param.getId());
            }
            if(!StringUtils.isEmpty(param.getUsername())){
                lambda.eq(UUser::getUsername, param.getUsername());
            }
            if(!StringUtils.isEmpty(param.getNickname())){
                lambda.eq(UUser::getNickname, param.getNickname());
            }
            if(!StringUtils.isEmpty(param.getPassword())){
                lambda.eq(UUser::getPassword, param.getPassword());
            }
            if(!StringUtils.isEmpty(param.getAuth())){
                lambda.eq(UUser::getAuth, param.getAuth());
            }
            if(!StringUtils.isEmpty(param.getUsertype())){
                lambda.eq(UUser::getUsertype, param.getUsertype());
            }
            if(!StringUtils.isEmpty(param.getUserstatus())){
                lambda.eq(UUser::getUserstatus, param.getUserstatus());
            }
            if(!CollectionUtils.isEmpty(param.getYxkssjList())){
                lambda.ge(UUser::getYxkssj, param.getYxkssjList().get(0));
                lambda.le(UUser::getYxkssj, param.getYxkssjList().get(1));
            }
            if(!CollectionUtils.isEmpty(param.getYxjzsjList())){
                lambda.ge(UUser::getYxjzsj, param.getYxjzsjList().get(0));
                lambda.le(UUser::getYxjzsj, param.getYxjzsjList().get(1));
            }
            if(!CollectionUtils.isEmpty(param.getMmyxsjList())){
                lambda.ge(UUser::getMmyxsj, param.getMmyxsjList().get(0));
                lambda.le(UUser::getMmyxsj, param.getMmyxsjList().get(1));
            }
            if(!StringUtils.isEmpty(param.getIp())){
                lambda.eq(UUser::getIp, param.getIp());
            }
            if(!StringUtils.isEmpty(param.getMac())){
                lambda.eq(UUser::getMac, param.getMac());
            }
            if(!CollectionUtils.isEmpty(param.getZhdlsjList())){
                lambda.ge(UUser::getZhdlsj, param.getZhdlsjList().get(0));
                lambda.le(UUser::getZhdlsj, param.getZhdlsjList().get(1));
            }
            if(!CollectionUtils.isEmpty(param.getJssjList())){
                lambda.ge(UUser::getJssj, param.getJssjList().get(0));
                lambda.le(UUser::getJssj, param.getJssjList().get(1));
            }
            if(!StringUtils.isEmpty(param.getMmcwsj())){
                lambda.eq(UUser::getMmcwsj, param.getMmcwsj());
            }
            if(!CollectionUtils.isEmpty(param.getCjsjList())){
                lambda.ge(UUser::getCjsj, param.getCjsjList().get(0));
                lambda.le(UUser::getCjsj, param.getCjsjList().get(1));
            }
            if(!StringUtils.isEmpty(param.getCjr())){
                lambda.eq(UUser::getCjr, param.getCjr());
            }
            if(!StringUtils.isEmpty(param.getSort())){
                lambda.eq(UUser::getSort, param.getSort());
            }
            if(!StringUtils.isEmpty(param.getRId())){
                lambda.eq(UUser::getRId, param.getRId());
            }
            if(!StringUtils.isEmpty(param.getDeleted())){
                lambda.eq(UUser::getDeleted, param.getDeleted());
            }
            if(!StringUtils.isEmpty(param.getRybh())){
                lambda.eq(UUser::getRybh, param.getRybh());
            }
            if(!StringUtils.isEmpty(param.getZw())){
                lambda.eq(UUser::getZw, param.getZw());
            }
            if(!StringUtils.isEmpty(param.getXzqh())){
                lambda.eq(UUser::getXzqh, param.getXzqh());
            }
            if(!StringUtils.isEmpty(param.getXzqhmc())){
                lambda.eq(UUser::getXzqhmc, param.getXzqhmc());
            }
            if(!StringUtils.isEmpty(param.getSjbh())){
                lambda.eq(UUser::getSjbh, param.getSjbh());
            }
            if(!StringUtils.isEmpty(param.getBmbm())){
                lambda.eq(UUser::getBmbm, param.getBmbm());
            }
            if(!StringUtils.isEmpty(param.getEnid())){
                lambda.eq(UUser::getEnid, param.getEnid());
            }
            if(!StringUtils.isEmpty(param.getEnname())){
                lambda.eq(UUser::getEnname, param.getEnname());
            }
            if(!StringUtils.isEmpty(param.getZzjgbm())){
                lambda.eq(UUser::getZzjgbm, param.getZzjgbm());
            }
            if(!StringUtils.isEmpty(param.getZzjgmc())){
                lambda.eq(UUser::getZzjgmc, param.getZzjgmc());
            }
            if(!StringUtils.isEmpty(param.getSfgly())){
                lambda.eq(UUser::getSfgly, param.getSfgly());
            }
            if(!StringUtils.isEmpty(param.getXiandm())){
                lambda.eq(UUser::getXiandm, param.getXiandm());
            }
            if(!StringUtils.isEmpty(param.getXianmc())){
                lambda.eq(UUser::getXianmc, param.getXianmc());
            }
            if(!StringUtils.isEmpty(param.getDsdm())){
                lambda.eq(UUser::getDsdm, param.getDsdm());
            }
            if(!StringUtils.isEmpty(param.getDsmc())){
                lambda.eq(UUser::getDsmc, param.getDsmc());
            }
            if(!StringUtils.isEmpty(param.getTel())){
                lambda.eq(UUser::getTel, param.getTel());
            }
            if(!StringUtils.isEmpty(param.getSsoSource())){
                lambda.eq(UUser::getSsoSource, param.getSsoSource());
            }
            if(!StringUtils.isEmpty(param.getSsoUsername())){
                lambda.eq(UUser::getSsoUsername, param.getSsoUsername());
            }
            if(!StringUtils.isEmpty(param.getDetailAddress())){
                lambda.eq(UUser::getDetailAddress, param.getDetailAddress());
            }
            if(!StringUtils.isEmpty(param.getAddress())){
                lambda.eq(UUser::getAddress, param.getAddress());
            }
            if(!StringUtils.isEmpty(param.getIdCard())){
                lambda.eq(UUser::getIdCard, param.getIdCard());
            }
            if(!StringUtils.isEmpty(param.getSex())){
                lambda.eq(UUser::getSex, param.getSex());
            }
            if(!StringUtils.isEmpty(param.getName())){
                lambda.eq(UUser::getName, param.getName());
            }
            if(!StringUtils.isEmpty(param.getMobile())){
                lambda.eq(UUser::getMobile, param.getMobile());
            }
            lambda.orderBy(true,false, UUser::getId);
        }


        /**
         * 请求model
         */
        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        private static class UUserReqVo extends UUser {
            private List<String> yxkssjList; // 有效开始时间起止
            private List<String> yxjzsjList; // 有效结束时间起止
            private List<String> mmyxsjList; // 密码失效日期起止
            private List<String> zhdlsjList; // 起止
            private List<String> jssjList; // 起止
            private List<String> cjsjList; // 创建时间起止
        }


}
